import { defineStore } from 'pinia';
import { ref } from 'vue';
import { chatApi } from '../services/chat.js';

export const useConversationStore = defineStore('conversation', () => {
  const conversations = ref([]);
  const loading = ref(false);
  
  async function loadConversations(uid) {
    if (!uid) return;
    
    loading.value = true;
    try {
      const list = await chatApi.listConversationPreviews(uid);
      conversations.value = list;
    } catch (e) {
      console.error('加载会话列表失败', e);
    } finally {
      loading.value = false;
    }
  }
  
  function addConversation(conversation) {
    conversations.value.unshift(conversation);
  }
  
  return {
    conversations,
    loading,
    loadConversations,
    addConversation
  };
});